<?php

use app\models\Agent;
use app\models\User;
use yii\helpers\Html;
use yii\helpers\Url;
use yii\widgets\ActiveForm;
use yii\widgets\Pjax;

/**
 * @var $this yii\web\View
 * @var $model app\models\CardDispatchForm
 */

$this->title = '分配卡';
$this->params['breadcrumbs'][] = '物联网卡管理';
$this->params['breadcrumbs'][] = $this->title;
?>
<?php $form = ActiveForm::begin();?>
<fieldset>
    <legend><label><input type="radio" name="CardDispatchForm[source]" value="range" checked="checked"> 设定起止卡号</label></legend>
    <?php echo $form->field($model, 'iccid_start')->hint('开始号码和结束号码必须同时填写。');?>
    <?php echo $form->field($model, 'iccid_end')->hint('开始号码结束号码和ICCID列表必须至少填写一项。');?>
</fieldset>
<?php echo $form->field($model, 'iccid_list')->textarea(['style' => 'min-height:200px;'])->label('<label><input type="radio" name="CardDispatchForm[source]" value="iccid"> 直接填写ICCID列表</label>');?>
<fieldset>
    <legend>分配用户</legend>
    <?php echo $form->field($model, 'user_type')->radioList(['user' => '用户', 'agent' => '代理商']);?>
    <?php echo $form->field($model, 'username')->hint('请填写需要分配给的用户名。');?>
    <?php Pjax::begin(['id' => 'pjax_user']);?>
    <?php $userType = Yii::$app->request->get('user_type');
    $username = Yii::$app->request->get('username');
    if (!empty($username)) {
        if ($userType == 'user') {
            /** @var User[] $userList */
            $userList = User::find()
                ->andWhere(['like', 'username', $username])
                ->andWhere(['status' => User::STATUS_OK])
                ->andFilterWhere(['agent_id' => empty($model->agent) ? null : $model->agent->id])
                ->all();
            if (!empty($userList)) {?>
                <div class="form-group">
                    <label class="control-label" for="carddispatchform-iccid_list">搜索到的用户</label>
                    <div class="help-block">
                        <?php foreach ($userList as $user) {?>
                            <button type="button" class="btn btn-info btn-sm" onclick="$('[name=\'CardDispatchForm[username]\']').val('<?php echo $user->username;?>');"><?php echo Html::encode($user->username);?></button>
                        <?php }?>
                    </div>
                </div>
            <?php } else {?>
                <div class="form-group">
                    <div class="help-block">没有找到用户：<?php echo Html::encode($username);?></button></div>
                </div>
            <?php }
        } elseif ($userType == 'agent') {
            /** @var Agent[] $agentList */
            $agentList = Agent::find()
                ->andWhere(['like', 'username', $username])
                ->andWhere(['status' => Agent::STATUS_OK])
                ->andFilterWhere(['pid' => empty($model->agent) ? null : $model->agent->id])
                ->all();
            if (!empty($agentList)) {?>
                <div class="form-group">
                    <label class="control-label" for="carddispatchform-iccid_list">搜索到的代理商</label>
                    <div class="help-block">
                        <?php foreach ($agentList as $agent) {?>
                            <button type="button" class="btn btn-info btn-sm" onclick="$('[name=\'CardDispatchForm[username]\']').val('<?php echo $agent->username;?>');"><?php echo Html::encode($agent->username);?></button>
                        <?php }?>
                    </div>
                </div>
            <?php } else {?>
                <div class="form-group">
                    <div class="help-block">没有找到代理商：<?php echo Html::encode($username);?></button></div>
                </div>
            <?php }
        }
    }?>
    <?php Pjax::end();?>
</fieldset>
<div class="form-group">
    <div class="col-lg-offset-1 col-lg-11">
        <button type="button" class="btn btn-default" onclick="window.history.go(-1);"><i class="ace-icon fa fa-arrow-left bigger-110"></i>返回</button>
        <button class="btn btn-primary"><i class="ace-icon fa fa-check bigger-110"></i>保存</button>
        <button type="reset" class="btn btn-warning"><i class="ace-icon fa fa-undo bigger-110"></i>重置</button>
    </div>
</div>
<?php $form->end();?>
<script>
    function pageInit() {
        $('[name="CardDispatchForm[search_api_id]"]').change(function () {
            var id = $(this).val();
            $.pjax.reload('#pjax_isp', {
                history: false,
                push: false,
                url: '<?php echo Url::current(['ajax' => 1]);?>&search_api_id=' + id
            });
        });
        $('[name="CardDispatchForm[username]"]').keyup(function () {
            var username = $(this).val();
            $.pjax.reload('#pjax_user', {
                history: false,
                push: false,
                url: '<?php echo Url::current(['ajax' => 1]);?>&user_type=' + $('[name="CardDispatchForm[user_type]"]:checked').val() + '&username=' + encodeURIComponent(username)
            });
        });
    }
</script>
